package com.lkk.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import com.lkk.data.ExcelDataEntity;
import com.lkk.data.Log;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;

/**
 * 读取excel工具类
 * @author admin
 *
 */
public class Red_Excel {
	//excel标题
	public static String excelTitle;
	/**
	 * 读取excel数据
	 * @param filePath excel文件路径
	 * @return 返回excel实体类对象的集合
	 */
	public List<ExcelDataEntity> redExcel(String filePath) {
		List<ExcelDataEntity> exDataEntityList = new ArrayList<ExcelDataEntity>();
		//判断文件是否是excel格式
		if(!filePath.endsWith(".xls")&&!filePath.endsWith(".xlsx")){
			System.out.println("文件不是excel类型");
		}
		//获取文件对象
		File file = new File(filePath);
        Workbook workbook = null;
		try {
			workbook = WorkbookFactory.create(file);
		} catch (EncryptedDocumentException e) {
			Log.error(e.getMessage());
		} catch (InvalidFormatException e) {
			Log.error(e.getMessage());
		} catch (IOException e) {
			Log.error(e.getMessage());
		}

        //得到第一个工作表
        Sheet sheet = workbook.getSheetAt(0);
        
        //获得表的标题
        excelTitle = 
        		sheet.getRow(Constant.TITLEROW).
        		getCell(Constant.TITLECELL).getStringCellValue();
        
        //获得excel有数据的最大行数
        int totalRowNum = sheet.getLastRowNum();
        
       //获得实体类集合
        for(int i = Constant.STARTCELL ; i <= totalRowNum ; i++){
        	//调用同类的获取行实体对象方法获取行对象
        	ExcelDataEntity exEntity = this.getExcelEntity(sheet, i);
        	exDataEntityList.add(exEntity);
        }
        return exDataEntityList;
    }
	/**
	 * 获取指定行的实体类对象
	 * @param sheet
	 * @param rowNum
	 */
	public ExcelDataEntity getExcelEntity(Sheet sheet,int rowNum) {
		ExcelDataEntity exEntity = new ExcelDataEntity();
		//根据行数获取具体列的数据
		Row row = sheet.getRow(rowNum);
		List<String> exArrays = new ArrayList<String>();
		for(int i=0;i<Constant.TOTALCELLNUM;i++) {
			//获取具体列的列对象
			Cell cell = row.getCell(i);
			String cellResult;
			if(cell.getCellType()==HSSFCell.CELL_TYPE_NUMERIC) {
				int num = (int)cell.getNumericCellValue();
				cellResult = Integer.toString(num);
			}else {
				//获取具体列对象的String数据
				cellResult = cell.getStringCellValue().toString();
			}
			exArrays.add(cellResult);
		}
		exEntity.setCaseNum(exArrays.get(0));
		exEntity.setCaseName(exArrays.get(1));
		exEntity.setProtocol(exArrays.get(2));
		exEntity.setMethod(exArrays.get(3));
		exEntity.setHeader(exArrays.get(4));
		exEntity.setUrl(exArrays.get(5));
		exEntity.setIfEncrypt(exArrays.get(6));
		exEntity.setIfCookie(exArrays.get(7));
		exEntity.setIfToken(exArrays.get(8));
		exEntity.setBody(exArrays.get(9));
		exEntity.setAssertions(exArrays.get(10));
		exEntity.setStatus(exArrays.get(11));
		return exEntity;
	}
	
}
